<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">领货订单编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="cabCode" class="layui-input" type="text" placeholder="请输入领货订单编号"/>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="orderReceiveBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="orderReceiveTable" lay-filter="orderReceiveTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="orderReceiveTableBar">
    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    -->
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="orderReceiveForm">
    <form lay-filter="orderReceiveForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">出库组件</label>
            <div class="layui-input-block">
                <table class="layui-table" id="componentTable" lay-filter="componentTable">
                    <thead>
                    <tr style="">
                        <td style="">上货类型</td>
                        <td style="">配货需求单</td>
                        <td style="">上货计划单</td>
                        <td style="">物料数量</td>
                        <td style="">提交时间</td>
                        <td style="">提交人</td>
                        <td style="">操作</td>
                    </tr>
                    <tbody id="inventorOutJobPrintTable">
                    <tr class="layui-form componentTr" >
                        <td id="typeTd" style="width: 200px;table-layout: fixed" class="typeTd">
                            <select name="type" lay-filter="type">
                                <option value="replenishment">补货上货</option>
                                <option value="selfLoad">自主上货</option>
                            </select>
                        </td>
                        <td id="demandIdTd" style="width: 200px;table-layout: fixed" class="demandIdTd">

                        </td>
                        <td id="loadPlanIdTd" style="width: 200px;table-layout: fixed" class="loadPlanIdTd">
                            <select name="loadPlanId" id="loadPlanId" lay-filter="loadPlanId" lay-verify="required" required></select>
                        </td>
                        <td id="skuNumTd" class="skuNumTd"></td>
                        <td id="creatorTd" class="creatorTd"></td>
                        <td id="createTimeTd" class="createTimeTd"></td>
                        <td style="width: 50px;table-layout: fixed" class="layui-table-cell"  align="center">
                            <button id="recieveBtn" class="layui-btn layui-btn-xs receiveBtn" type="button" lay-filter="receiveBtn"  formnovalidate>领货</button>
                            </td>
                    </tr>

                    </tbody>

                </table>

                <br>
                <table class="layui-table" id="deliveryDetailTable" lay-filter="deliveryDetailTable"></table>
            </div>
        </div>
        <!--
        <div class="layui-form-item">
            <label class="layui-form-label">上货类型</label>
            <div class="layui-input-block">
                <select name="type">
                    <option value="replenishment">补货上货</option>
                    <option value="selfLoad">自主上货</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item" id="demandIdDiv">
            <label class="layui-form-label">配货需求单</label>
            <div class="layui-input-block">
                <select name="demandId" id="demandId" lay-filter="demandId" lay-verify="required" required></select>
            </div>
        </div>

        <div class="layui-form-item" id="loadPlanIdDiv">
            <label class="layui-form-label">上货计划单</label>
            <div class="layui-input-block">
                <select name="loadPlanId" id="loadPlanId" lay-filter="loadPlanId" lay-verify="required" required></select>
            </div>
        </div>
        -->


        <div class="layui-form-item text-center">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="orderReceiveFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>

<script type="text/html" id="detailTab">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <table class="layui-table" id="orderReceiveTable4Detail" lay-filter="orderReceiveTable4Detail"></table>
                <br>
                <!-- 表格 -->
                <table class="layui-table" id="orderReceiveDetailTable" lay-filter="orderReceiveDetailTable"></table>
            </div>
        </div>
    </div>

</script>

<script type="text/html" id="orderReceiveDetailTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="labelDetail">条码列表</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="loadDetail">上货详情</a>
</script>


<script type="text/html" id="loadTable">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->

                <!-- 表格 -->
                <table class="layui-table" id="orderReceiveLoadTable" lay-filter="orderReceiveLoadTable"></table>
            </div>
        </div>
    </div>
</script>

<script type="text/html" id="labelTable">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->

                <!-- 表格 -->
                <table class="layui-table" id="orderReceiveLabelTable" lay-filter="orderReceiveLabelTable"></table>
            </div>
        </div>
    </div>
</script>


<script type="text/html" id="deliveryDetailTableBar">
    {{# if(d.status=='done'){ }}
    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="distributeRecordLabel">配货记录</a>
    -->
    {{#} }}
    {{# if(d.status!='done'){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="receive">领货</a>
    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="distributeRecordLabel">配货记录</a>
    -->
    {{#} }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
</script>




<script type="text/html" id="distributionTab2">

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar" id="packLabelCodeDiv">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">包装单号：</label>
                            <div class="layui-input-inline mr0">
                                <input id="packLabelCode2" name="packLabelCode" class="layui-input" type="text" placeholder="请输入包装编号"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" id="addPackLabel2" lay-filter="addPackLabel2" lay-submit>确定</button>
                        </div>
                    </div>
                </div>
                <!-- 表格
                <table class="layui-table" id="distributionTable2" lay-filter="distributionTable2"></table>
                <br>
                -->
                <table class="layui-table" id="distributionLabelTable2" lay-filter="distributionLabelTable2"></table>
                <br>
                <!--
                <div class="page" style="padding-bottom: 25px;">
                    <div align="center" id="operationDiv2">
                        <div  class="layui-btn"  lay-filter="distributionSubmit2" lay-submit>提交</div>
                    </div>
                </div>
                -->
            </div>
        </div>
    </div>

</script>

<script type="text/html" id="distributionLabelTableBar">
    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改数量</a>
    -->
    {{# if(d.status!='receive'){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="receive">领货确认</a>
    <!--
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    -->
    {{#} }}

</script>



<script type="text/html" id="distributionTab3">

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar" id="packLabelCodeDiv3">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">包装编号：</label>
                            <div class="layui-input-inline mr0">
                                <input id="packLabelCode3" name="packLabelCode" class="layui-input" type="text" placeholder="请输入包装编号"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" id="addPackLabel3" lay-filter="addPackLabel2" lay-submit>搜索</button>
                        </div>
                    </div>
                </div>
                <!-- 表格
                <table class="layui-table" id="distributionTable3" lay-filter="distributionTable2"></table>
                <br>
                -->
                <table class="layui-table" id="distributionLabelTable3" lay-filter="distributionLabelTable2"></table>
                <br>
                <!--
                <div class="page" style="padding-bottom: 25px;">
                    <div align="center" id="operationDiv2">
                        <div  class="layui-btn"  lay-filter="distributionSubmit2" lay-submit>提交</div>
                    </div>
                </div>
                -->
            </div>
        </div>
    </div>

</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#orderReceiveTable',
            url: config.base + 'enterprisecab/order/admin/orderReceive/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}

                ,{field: 'code', sort: true, title: '领货单编号'}
                ,{field: 'demandCode', sort: true, title: '配货需求单编号'}
                ,{field: 'loadPlanCode', sort: true, title: '上货计划编号'}
                ,{field: 'typeDesc', sort: true, title: '上货计划类型'}
                ,{field: 'skuQuantity', sort: true, title: '物料数量'}
                ,{field: 'warehouseName', sort: true, title: '仓库'}
                ,{field: 'statusDesc', sort: true, title: '状态'}
                ,{field: 'createTime', sort: true, title: '提交时间'}
                ,{field: 'creatorName', sort: true, title: '提交人'}
                ,{align: 'center', toolbar: '#orderReceiveTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(orderReceiveTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }else if(obj.event=='detail'){
                showDetailModel(data);
            }
        });

        //监听排序
        table.on('sort(orderReceiveTable)', function(obj){
            table.reload('orderReceiveTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 添加按钮点击事件
        $('#orderReceiveBtnAdd').click(function () {
            showEditModel();
        });

        function getReceiveTable(id){
            var insTb = table.render({
                elem: '#orderReceiveTable4Detail',
                url: config.base + 'enterprisecab/order/admin/orderReceive/list?id='+id,
                page: false,
                cellMinWidth: 100,
                cols: [[
                    {type: 'checkbox'}

                    ,{field: 'code', sort: true, title: '领货单编号'}
                    ,{field: 'demandCode', sort: true, title: '配货需求单编号'}
                    ,{field: 'loadPlanCode', sort: true, title: '上货计划编号'}
                    ,{field: 'typeDesc', sort: true, title: '上货计划类型'}
                    ,{field: 'skuQuantity', sort: true, title: '物料SKU数量'}
                    ,{field: 'warehouseName', sort: true, title: '仓库'}
                    ,{field: 'statusDesc', sort: true, title: '状态'}
                    ,{field: 'createTime', sort: true, title: '提交时间'}
                    ,{field: 'creatorName', sort: true, title: '提交人'}
                    // ,{align: 'center', toolbar: '#orderReceiveTableBar', title: '操作', width: 200}
                ]],
                done: function (res, curr, count) {
                    //移除按钮
                    var menuButton = config.getMenuButton(location.hash);
                    if(menuButton.indexOf("搜索") == -1){
                        $('.layui-form.toolbar').remove();
                    }
                    if(menuButton.indexOf("修改") == -1){
                        $("[lay-event='edit']").remove();
                    }
                    if(menuButton.indexOf("删除") == -1){
                        $("[lay-event='del']").remove();
                    }
                }
            });
        }

        function getDetailTable(receiveId){
            // 渲染表格
            var insTb = table.render({
                elem: '#orderReceiveDetailTable',
                url: config.base + 'enterprisecab/order/admin/orderReceiveDetail/list?receiveId='+receiveId,
                page: true,
                cellMinWidth: 100,
                cols: [[
                    {type: 'checkbox'}

                    ,{field: 'skuCode', sort: true, title: '物料编号'}
                    ,{field: 'skuName', sort: true, title: '物料名称'}
                    ,{field: 'skuSpec', sort: true, title: '物料规格'}
                    ,{field: 'productBrandName', sort: true, title: '品牌'}
                    ,{field: 'unitName', sort: true, title: '物料单位'}
                    ,{field: 'marketPrice', sort: true, title: '物料单价'}
                    ,{field: 'quantityConfirm', sort: true, title: '需求数量'}
                    ,{field: 'quantityReceive', sort: true, title: '领取数量'}
                    ,{field: 'creatorName', sort: true, title: '领取人'}
                    ,{field: 'createTime', sort: true, title: '领取时间'}
                    ,{align: 'center', toolbar: '#orderReceiveDetailTableBar', title: '操作', width: 200}
                ]],
                done: function (res, curr, count) {
                    //移除按钮
                    var menuButton = config.getMenuButton(location.hash);
                    if(menuButton.indexOf("搜索") == -1){
                        $('.layui-form.toolbar').remove();
                    }
                    if(menuButton.indexOf("修改") == -1){
                        $("[lay-event='edit']").remove();
                    }
                    if(menuButton.indexOf("删除") == -1){
                        $("[lay-event='del']").remove();
                    }
                }
            });

            // 工具条点击事件
            table.on('tool(orderReceiveDetailTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') { //修改
                    showEditModel(data);
                } else if (obj.event === 'del') { // 删除
                    doDelete(obj);
                }else if(obj.event=='loadDetail'){
                    showLoadModel(data);
                }else if(obj.event=='labelDetail'){
                    showLabelModel(data);
                }
            });

            //监听排序
            table.on('sort(orderReceiveDetailTable)', function(obj){
                table.reload('orderReceiveDetailTable', {
                    initSort: obj
                    ,where: {
                        sort: obj.field
                        ,order: obj.type
                    }
                });
            });
        }

        function getLoadTable(receiveDetailId){
            // 渲染表格
            var insTb = table.render({
                elem: '#orderReceiveLoadTable',
                url: config.base + 'enterprisecab/order/admin/orderReceiveLoad/list?receiveDetailId='+receiveDetailId,
                page: true,
                cellMinWidth: 100,
                cols: [[
                    {type: 'checkbox'}
                    ,{field: 'code', sort: true, title: '上货记录编号'}
                    ,{field: 'skuCode', sort: true, title: '物料编码'}
                    ,{field: 'skuName', sort: true, title: '物料名称'}
                    ,{field: 'skuSpec', sort: true, title: '物料规格'}
                    ,{field: 'productBrandName', sort: true, title: '品牌'}
                    ,{field: 'unitName', sort: true, title: '物料单位'}
                    ,{field: 'cabCode', sort: true, title: '柜子编号'}
                    ,{field: 'spaceCode', sort: true, title: '货道编号'}
                    ,{field: 'quantity', sort: true, title: '上货数量'}
                    ,{field: 'creatorName', sort: true, title: '上货人'}
                    ,{field: 'createTime', sort: true, title: '上货时间'}
                    ,{align: 'center', toolbar: '#orderReceiveLoadTableBar', title: '操作', width: 200}
                ]],
                done: function (res, curr, count) {
                    //移除按钮
                    var menuButton = config.getMenuButton(location.hash);
                    if(menuButton.indexOf("搜索") == -1){
                        $('.layui-form.toolbar').remove();
                    }
                    if(menuButton.indexOf("修改") == -1){
                        $("[lay-event='edit']").remove();
                    }
                    if(menuButton.indexOf("删除") == -1){
                        $("[lay-event='del']").remove();
                    }
                }
            });

            // 工具条点击事件
            table.on('tool(orderReceiveLoadTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') { //修改
                    showEditModel(data);
                } else if (obj.event === 'del') { // 删除
                    doDelete(obj);
                }
            });

            //监听排序
            table.on('sort(orderReceiveLoadTable)', function(obj){
                table.reload('orderReceiveLoadTable', {
                    initSort: obj
                    ,where: {
                        sort: obj.field
                        ,order: obj.type
                    }
                });
            });

            // 搜索
            form.on('submit(formSubSearchLog)', function (data) {
                insTb.reload({where: data.field,page: {curr: 1}},'data');
            });
        }

        function getLabelTable(receiveDetailId){
            // 渲染表格
            var insTb = table.render({
                elem: '#orderReceiveLabelTable',
                url: config.base + 'enterprisecab/order/admin/inventoryJobLabel/list?receiveDetailId='+receiveDetailId,
                page: true,
                cellMinWidth: 100,
                cols: [[
                    {type: 'checkbox'}
                    ,{field: 'packLabelCode', sort: true, title: '包装编码'}
                    ,{field: 'quantity', sort: true, title: '数量'}

                    ,{field: 'packName', sort: true, title: '包装规格名称'}
                    ,{field: 'skuCode', sort: true, title: '物料编码'}
                    ,{field: 'skuName', sort: true, title: '物料名称'}
                    ,{field: 'skuSpec', sort: true, title: '物料规格'}
                    ,{field: 'productBrandName', sort: true, title: '品牌'}
                    ,{field: 'unitName', sort: true, title: '物料单位'}
                    // ,{field: 'warehouseId', sort: true, title: '仓库id'}
                    // ,{field: 'areaId', sort: true, title: '库区id'}
                    // ,{field: 'shelfId', sort: true, title: '货架id'}
                    // ,{field: 'spaceId', sort: true, title: '货位id'}
                    // ,{field: 'boxId', sort: true, title: '料箱id'}
                    ,{field: 'receiverName', sort: true, title: '领取人'}
                    ,{field: 'receiveTime', sort: true, title: '领取时间'}
                    // ,{align: 'center', toolbar: '#orderReceiveLabelTableBar', title: '操作', width: 200}
                ]],
                done: function (res, curr, count) {
                    //移除按钮
                    var menuButton = config.getMenuButton(location.hash);
                    if(menuButton.indexOf("搜索") == -1){
                        $('.layui-form.toolbar').remove();
                    }
                    if(menuButton.indexOf("修改") == -1){
                        $("[lay-event='edit']").remove();
                    }
                    if(menuButton.indexOf("删除") == -1){
                        $("[lay-event='del']").remove();
                    }
                }
            });

            // 工具条点击事件
            table.on('tool(orderReceiveLabelTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') { //修改
                    showEditModel(data);
                } else if (obj.event === 'del') { // 删除
                    doDelete(obj);
                }
            });

            //监听排序
            table.on('sort(orderReceiveLabelTable)', function(obj){
                table.reload('orderReceiveLabelTable', {
                    initSort: obj
                    ,where: {
                        sort: obj.field
                        ,order: obj.type
                    }
                });
            });
        }

        function showDetailModel(data){
            admin.open({
                type: 1,
                area: '1440px',
                offset: '65px',
                title: '详情',
                content: $('#detailTab').html(),
                success: function () {
                    getReceiveTable(data.id);
                    getDetailTable(data.id);
                }
            })
        }

        function showLoadModel(data){
            admin.open({
                type: 1,
                area: '1440px',
                offset: '65px',
                title: '上货列表',
                content: $('#loadTable').html(),
                success: function () {
                    getLoadTable(data.id);
                }
            })
        }

        function showLabelModel(data){
            admin.open({
                type: 1,
                area: '1440px',
                offset: '65px',
                title: '条码列表',
                content: $('#labelTable').html(),
                success: function () {
                    getLabelTable(data.id);
                }
            })
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            var receiveConfirmLabels=[];
            var receiveConfirmLabelMap=new Map();
            var loadPlanData={};
            var loadPlanDetails=[];
            admin.open({
                type: 1,
                area: ['1640px','560px'],
                offset: '65px',
                title: data ? '修改领货单' : '添加领货单',
                content: $('#orderReceiveForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function(index, item){
                            if(data.status  == item.text){
                                data.status = item.value;
                            }
                        });
                    }

                    function showDeliveryLabelTable(deliveryData,orderDetailData){
                        admin.open({
                            type: 1,
                            area: '1500px',
                            offset: '65px',
                            title: deliveryData.eventType=='detail' ? '详情' : '领货',
                            content: $('#distributionTab2').html(),
                            success: function () {
                                if(deliveryData.eventType=='editQuantity'){
                                    $("#packLabelCodeDiv").hide();
                                }
                                // deliveryTable(deliveryData,'distributionTable2');
                                // showDistributeRecordLabelTable(deliveryData,'distributionLabelTable2',orderDetailData)
                                // deliveryLabelTable(deliveryData,orderDetailData,'distributionLabelTable2');
                                distributeLabelTable(deliveryData,'distributionLabelTable2',orderDetailData)
                            }
                        })

                    }

                    $(".receiveBtn").on('click',function(data){
                        var type=$("form select[name='type']").val();
                        var loadPlanId=$("#loadPlanId").val();
                        if(loadPlanId==''||loadPlanId==null){
                            layer.msg('请选择上货计划单', {icon: 2});
                            return
                        }
                        loadPlanData={type:type,loadPlanId:loadPlanId};
                        loadPlanData.eventType='distribute1'
                        showDeliveryLabelTable(loadPlanData)
                    })


                    function showDeliveryDetailLabelTable(deliveryData,orderDetailData){
                        admin.open({
                            type: 1,
                            area: '1500px',
                            offset: '65px',
                            title: deliveryData.eventType=='detail' ? '详情' : '领货',
                            content: $('#distributionTab3').html(),
                            success: function () {
                                if(deliveryData.eventType=='editQuantity'){
                                    $("#packLabelCodeDiv").hide();
                                }
                                // deliveryTable(deliveryData,'distributionTable2');
                                // showDistributeRecordLabelTable(deliveryData,'distributionLabelTable2',orderDetailData)
                                // deliveryLabelTable(deliveryData,orderDetailData,'distributionLabelTable2');
                                distributeLabelTable(deliveryData,'distributionLabelTable3',orderDetailData)
                            }
                        })

                    }


                    function showDeliveryLabelTable(deliveryData,orderDetailData){
                        admin.open({
                            type: 1,
                            area: '1500px',
                            offset: '65px',
                            title: deliveryData.eventType=='detail' ? '详情' : '领货',
                            content: $('#distributionTab2').html(),
                            success: function () {
                                if(deliveryData.eventType=='editQuantity'){
                                    $("#packLabelCodeDiv").hide();
                                }
                                // deliveryTable(deliveryData,'distributionTable2');
                                // showDistributeRecordLabelTable(deliveryData,'distributionLabelTable2',orderDetailData)
                                // deliveryLabelTable(deliveryData,orderDetailData,'distributionLabelTable2');
                                distributeLabelTable(deliveryData,'distributionLabelTable2',orderDetailData)
                            }
                        })

                    }


                    function distributeLabelTable(data,elemId,orderDetailData){

                        // var detailSkuMap=new Map();
                        // admin.req('platformCab/order/admin/orderDistributionDetail/list?distId='+data.id,{limit:9999}, function (res){
                        //     if(res.code == 200){
                        //         res.data.forEach(item=>{
                        //             item.quantitySurplus=item.quantityConfirm-item.quantityDistributed
                        //             detailSkuMap.set(item.skuCode,item);
                        //         })
                        //     }else {
                        //
                        //     }
                        // }, 'GET',false)

                        var insTb ;
                        var reloadLabelTable=function(labelData,eventType){
                            var cols=[
                                {type: 'checkbox'}
                                // ,{field: 'skuCode', sort: true, title: '物料编号'}
                                // ,{field: 'productSpecCode', sort: true, title: '物料规格'}
                                ,{field: 'packLabelCode', sort: true, title: '包装编码'}
                                ,{field: 'productName', sort: true, title: '物料名称'}
                                ,{field: 'packSpecCode', sort: true, title: '包装规格'}
                                ,{field: 'unitName', sort: true, title: '单位'}
                                ,{field: 'baseUnitName', sort: true, title: '最小单位'}
                                ,{field: 'baseUnitNum', sort: true, title: '最小单位数'}
                                ,{field: 'isScan', sort: true, title: '是否已扫描',templet:function (d) {
                                        if(d.isScan4Receive||d.status=='receive'){
                                            return '是'
                                        }
                                        return '否'
                                    }}
                                ,{field: 'packLabelQuantity', sort: true, title: '配货数',templet:function (d) {
                                        // return d.storageQuantity;
                                        return d.packLabelQuantity;
                                    }}
                                // ,{field: 'quantityReceive', sort: true, title: '领取数',templet:function (d) {
                                //         // if(d.status=='created'){
                                //         //     return '<div><input id="quantity-' +d.packLabelId+'-'+d.skuCode+'" type="text" value="'+d.packLabelQuantity+'" class="layui-input quantity" lay-verify="number" value=""/></div>';
                                //         // }
                                //         // return d.quantityReceive;
                                //         return d.packLabelQuantity;
                                //     }}
                                ,{field: 'createTime', sort: true, title: '创建时间'}


                            ]

                            if(eventType!='detail'){
                                var colOp={align: 'center', toolbar: '#distributionLabelTableBar', title: '操作', width: 200};
                                cols.push(colOp);
                            }

                            insTb=table.render({
                                elem: '#'+elemId,
                                // url: config.base + 'platformCab/order/admin/orderDistributionLabel/list?demandId='+data.id,
                                data:labelData,
                                page: false,
                                cellMinWidth: 100,
                                cols: [cols],
                                done: function (res, curr, count) {
                                    //移除按钮
                                    /**
                                     var menuButton = config.getMenuButton(location.hash);
                                     if(menuButton.indexOf("搜索") == -1){
                            $('.layui-form.toolbar').remove();
                        }
                                     if(menuButton.indexOf("修改") == -1){
                            $("[lay-event='edit']").remove();
                        }
                                     if(menuButton.indexOf("删除") == -1){
                            $("[lay-event='del']").remove();
                        }*/
                                }
                            });
                        }
                        var labelDatas=[];
                        var labelMap=new Map();
                        var getLabelData=function(data,orderDetailData){
                            // var params={};
                            // if(orderDetailData){
                            //     params.skuCode=orderDetailData.skuCode;
                            // }
                            // platformCab/order/admin/orderDistributionLabel/list?distId='+deliveryData.distId+'&deliveryId='+deliveryData.id
                            var skuCode=undefined;
                            if(orderDetailData){
                                skuCode=orderDetailData.skuCode;
                            }
                            var queryFlag=undefined;
                            if(data.eventType=='detail'){
                                queryFlag='queryReceiveLabel';
                            }
                            admin.req('enterprisecab/order/admin/inventoryJobLabel/list',{
                                // distId:data.distId,
                                loadPlanId:data.loadPlanId,skuCode:skuCode,queryFlag:queryFlag,status:'inventoryOut'}, function (res){
                                if(res.code == 200){
                                    labelDatas=res.data;
                                    labelDatas.forEach(item=>{
                                        item.packLabelQuantity=item.storageQuantity;
                                        var receivedConfirmLabel=receiveConfirmLabelMap.get(item.packLabelCode)
                                        if(receivedConfirmLabel){
                                            item.isScan4Receive=true;
                                            item.status='receive';
                                        }
                                    })
                                }else {

                                }
                            }, 'GET',false)
                        }
                        getLabelData(data,orderDetailData);
                        labelDatas=labelDatas.filter(item=>{
                            if(item.status=='deleted'){
                                return false;
                            }
                            labelMap.set(item.packLabelCode,item);
                            return true;
                        })
                        // labelDatas.forEach(item=>{
                        //     labelMap.set(item.packLabelCode,item);
                        // })
                        reloadLabelTable(labelDatas,data.eventType);
                        // 工具条点击事件
                        table.on('tool('+elemId+')', function (obj) {
                            var data = obj.data;
                            if (obj.event === 'edit') { //修改
                                // showEditModel(data);
                            } else if (obj.event === 'del') { // 删除
                                del(obj);
                            }else if(obj.event=='receive'){
                                receiveConfirm(data.id,data.packLabelId,data);
                            }
                        });

                        function receiveConfirm(id,packLabelId,data){
                            if(data.isScan4Receive=undefined||!data.isScan4Receive){
                                layer.msg('请先扫描此包装条码再确认领货', {icon: 2});
                                return false;
                            }
                            layer.msg('已确认', {icon: 1});
                            labelDatas.forEach(item=>{
                                if(item.packLabelId==packLabelId){
                                    item.status='receive';
                                    receiveConfirmLabels.push(item);
                                    receiveConfirmLabelMap.set(item.packLabelCode,item);
                                    setReceiveQuantityTotal(item);
                                }
                            })
                            reloadLabelTable(labelDatas);
                            reloadLoadPlanDetailTable(loadPlanData,loadPlanDetails);
                        }

                        function receive(id,packLabelId,data){
                            if(data.isScan4Receive=undefined||!data.isScan4Receive){
                                layer.msg('请先扫描此包装条码再确认领货', {icon: 2});
                                return false;
                            }
                            var distributedData=[];
                            var labelQuantityEditMap=new Map();
                            $(".quantity").each(function(index,item){
                                var id=item.id;
                                var startIndex=id.indexOf("-");
                                var endIndex=id.indexOf("-",startIndex+1);
                                var labelId=id.substr(startIndex+1,(endIndex-startIndex-1));
                                var skuCode=id.substr(endIndex+1,id.length-endIndex-1);
                                var eleNo=id.substr("quantity-".length,id.length);
                                var val=$(this).val();
                                distributedData.push({packLabelId:labelId,skuCode:skuCode,packLabelQuantity:val})
                                labelQuantityEditMap.set(labelId,val);
                            })




                            var labelQuantityErrorMsg;
                            labelDatas.forEach(item=>{
                                if(item.packLabelId==packLabelId){
                                    var newQuantity=labelQuantityEditMap.get(item.packLabelId);
                                    if(newQuantity){
                                        item.quantityReceive=parseInt(newQuantity);
                                    }

                                    if(item.quantityReceive>item.packLabelQuantity){
                                        // layer.msg('修改后的数量['+item.packLabelQuantity+']不能大于包装条码['+item.packLabelCode+']对应的最大数量['+item.quantitySurplus+']',{icon: 2})
                                        // return false;
                                        labelQuantityErrorMsg='领货的确认数量['+item.quantityReceive+']不能大于包装条码['+item.packLabelCode+']已配货的最大数量['+item.packLabelQuantity+']';
                                    }
                                }
                            })
                            if(labelQuantityErrorMsg){
                                layer.msg(labelQuantityErrorMsg,{icon: 2})
                                return false;
                            }

                            admin.req('enterprisecab/order/admin/orderDelivery/receive/' +id, {quantityReceive:labelQuantityEditMap.get(packLabelId)}, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    labelDatas.forEach(item=>{
                                        if(item.packLabelId==packLabelId){
                                            item.status='receive';
                                        }
                                    })
                                    reloadLabelTable(labelDatas);
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'GET');

                        }

                        function del(obj) {
                            layer.confirm('确定要删除吗？', {
                                offset: '65px',
                                skin: 'layui-layer-admin'
                            }, function (i) {
                                layer.close(i);
                                layer.load(2);
                                var delData=function(){
                                    labelMap.delete(obj.data.packLabelCode);
                                    layer.msg('删除成功', {icon: 1});
                                    obj.del();
                                    labelDatas=labelDatas.filter(item=>{
                                        if(item.packLabelCode==obj.data.packLabelCode){
                                            return false;
                                        }
                                        return true;
                                    })
                                    reloadLabelTable(labelDatas);
                                }
                                if(obj.data.id){
                                    /**
                                    admin.req('enterprisecab/order/admin/orderDistributionLabel/receiveDel/' + obj.data.id, {}, function (res) {
                                        layer.closeAll('loading');
                                        if (res.code == 200) {
                                            delData();
                                        } else {
                                            layer.msg(res.msg, {icon: 2});
                                        }
                                    }, 'GET');
                                     */
                                }else{
                                    layer.closeAll('loading');
                                    delData();
                                }

                            });
                        }

                        var updateIsScan=function(id){
                            admin.req('platformCab/order/admin/orderDistributionLabel/scanLabel/' + id, {}, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'GET');
                        }

                        var addLabel=function(labelCode){
                            // var labelCode=$("input[name='packLabelCode']").val();
                            if(labelCode){
                                // if(labelMap.get(labelCode)){
                                //     layer.msg('条码已扫描', {icon: 2});
                                //     return;
                                // }
                                admin.req('enterprise/product/admin/productPackLabel/getByCode', {labelCode:labelCode,loadPlanId:data.loadPlanId}, function (res) {

                                    if (res.code == 200) {

                                        if(res.data==''||res.data==undefined||labelMap.get(res.data.code)==undefined){
                                            layer.msg('此上货计划单没有出库此包装条码', {icon: 2});
                                            return;
                                        }
                                        if(res.data.status!='inventoryOut'){
                                            layer.msg('此包装条码未扫描出库', {icon: 2});
                                            return;
                                        }
                                        if(labelMap.get(res.data.code)&&labelMap.get(res.data.code).isScan4Receive){
                                            layer.msg('此包装条码已扫描', {icon: 2});
                                            return;
                                        }

                                        // updateIsScan();

                                        labelDatas.forEach(item=>{
                                            if(item.packLabelId==res.data.id){
                                                item.isScan4Receive=true;
                                                // item.status='receive'
                                            }
                                        })
                                        setLabelQuantity();
                                        // setReceiveQuantityTotal(loadPlanDetails);
                                        reloadLabelTable(labelDatas);
                                        // reloadLoadPlanDetailTable(loadPlanData,loadPlanDetails);
                                        // labelMap.set(res.data.code,obj);
                                        // layer.msg(res.msg, {icon: 1});
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }, 'GET');
                            }
                        }

                        var setLabelQuantity=function(){
                            var labelQuantityEditMap=new Map();
                            $(".quantity").each(function(index,item){
                                var id=item.id;
                                var startIndex=id.indexOf("-");
                                var endIndex=id.indexOf("-",startIndex+1);
                                var labelId=id.substr(startIndex+1,(endIndex-startIndex-1));
                                var skuCode=id.substr(endIndex+1,id.length-endIndex-1);
                                var eleNo=id.substr("quantity-".length,id.length);
                                var val=$(this).val();
                                labelQuantityEditMap.set(labelId,val);
                            })

                            labelDatas.forEach(item=>{
                                var newQuantity=labelQuantityEditMap.get(item.packLabelId);
                                if(newQuantity){
                                    item.quantityReceive=parseInt(newQuantity);
                                }
                            })
                        }

                        var setReceiveQuantityTotal=function(labelData){
                            loadPlanDetails.forEach(item=>{
                                if(item.skuCode==labelData.skuCode){
                                    if(item.quantityReceive){
                                        item.quantityReceive+=parseInt(labelData.packLabelQuantity);
                                    }else{
                                        item.quantityReceive=parseInt(labelData.packLabelQuantity);
                                    }
                                }
                            })
                        }

                        $("#addPackLabel"+elemId.substr("distributionLabelTable".length,1)).click(function(){
                            if(elemId=='distributionLabelTable3'){
                                var skuCode=undefined;
                                if(orderDetailData){
                                    skuCode=orderDetailData.skuCode;
                                }
                                admin.req('enterprisecab/order/admin/inventoryJobLabel/list',{
                                    // distId:data.distId,

                                    loadPlanId:data.loadPlanId,skuCode:skuCode,packLabelCode:$("#packLabelCode3").val(),queryFlag:'queryReceiveLabel',status:'inventoryOut'}, function (res){
                                    if(res.code == 200){
                                        labelDatas=res.data;
                                        reloadLabelTable(labelDatas,data.eventType);
                                    }else {

                                    }
                                }, 'GET',false)
                            }else{
                                var labelCode=$("#packLabelCode"+elemId.substr("distributionLabelTable".length,1)).val();
                                if(labelCode){
                                    addLabel(labelCode)
                                }else{
                                    layer.msg('请输入或扫描包装条码', {icon: 2});
                                }
                            }

                        })



                    }

                    function reloadLoadPlanDetailTable(deliveryData,loadPlanDetailData){
                        var insTb = table.render({
                            elem: '#deliveryDetailTable',
                            // url: config.base + 'enterprisecab/cabinet/admin/cabinetLoadPlan/cabinetLoadPlanDetailList?cabinetLoadPlanId='+deliveryData.loadPlanId+'&type='+deliveryData.type+'&limit=9999',
                            data:loadPlanDetailData,
                            page: false,
                            cellMinWidth: 100,
                            cols: [[
                                {type: 'checkbox'}
                                ,{field: 'skuCode', sort: true, title: '物料编号'}
                                ,{field: 'productName', sort: true, title: '物料名称'}
                                ,{field: 'skuSpecValueNames', sort: true, title: '物料规格'}
                                // ,{field: 'productSpecCode', sort: true, title: '物料规格'}
                                ,{field: 'unitName', sort: true, title: '单位'}
                                // ,{field: 'price', sort: true, title: '单价'}
                                ,{field: 'quantityStock', sort: true, title: '仓库库存'}
                                ,{field: 'brandName', sort: true, title: '品牌'}
                                // ,{field: 'statusDesc', sort: true, title: '状态'}
                                ,{field: 'planReplenishment', sort: true, title: '配货需求数'}
                                ,{field: 'quantity', sort: true, title: '计划补货数'}
                                // ,{field: 'quantityConfirm', sort: true, title: '补货数'}
                                // ,{field: 'quantityReceive', sort: true, title: '实配数'}
                                ,{field: 'quantityReceive', sort: true, title: '领取数'}
                                // ,{field: 'price', sort: true, title: '单价'}
                                // ,{field: 'total', sort: true, title: '总货值'}
                                // ,{field: 'brandName', sort: true, title: '品牌'}

                                // ,{field: 'createTime', sort: true, title: '创建时间'}

                                ,{align: 'center', toolbar: '#deliveryDetailTableBar', title: '操作', width: 200}
                            ]],
                            done: function (res, curr, count) {
                                //移除按钮

                            }
                        });

                        // 工具条点击事件
                        table.on('tool(deliveryDetailTable)', function (obj) {
                            var detailData = obj.data;
                            if (obj.event === 'edit') { //修改
                                // showEditModel(data);
                            } else if (obj.event === 'del') { // 删除
                                // doDelete(obj);
                            }else if(obj.event=='distributeRecordLabel'){
                                // data.eventType='editQuantity'
                                // deliveryData.distId=data.id;
                                // showDistributeRecordLabelTable(deliveryData,detailData);

                            }else if(obj.event=='receive'){
                                deliveryData.eventType='detailDistribute';
                                // showDistributeRecordLabelTable(deliveryData,detailData);
                                showDeliveryLabelTable(deliveryData,detailData);
                            }else if(obj.event=='detail'){
                                deliveryData.eventType='detail';
                                showDeliveryDetailLabelTable(deliveryData,detailData);
                            }
                        });
                    }

                    var getLoadPlanDetailTable=function(deliveryData){
                        admin.req('enterprisecab/cabinet/admin/cabinetLoadPlan/cabinetLoadPlanDetailList4receive', {cabinetLoadPlanId:deliveryData.loadPlanId,type:deliveryData.type,limit:999}, function (res) {

                            if (res.code == 200) {
                                loadPlanDetails=res.data;
                                reloadLoadPlanDetailTable(deliveryData,loadPlanDetails);
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET',false);
                    }

                    var getDemandHtml=function(){
                        var html='<select name="demandId" id="demandId" lay-filter="demandId" lay-verify="required" required></select>';
                        return html;
                    }
                    var getDistributionDemandData=function(){
                        admin.req('enterprisecab/order/admin/distributionDemand/list', {limit:999}, function (res) {

                            if (res.code == 200) {
                                var html='<option value="">请选择配货需求单</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" >'+res.data[k].distCode+'</option>'
                                }
                                $("#demandId").html(html);
                                form.render('select');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET',false);
                    }

                    var getLoadPlanData=function(demandId){
                        admin.req('enterprisecab/cabinet/admin/cabinetLoadPlan/list', {distributionDemandId:demandId,limit:999}, function (res) {

                            if (res.code == 200) {
                                var html='<option value="">请选择上货计划单</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-creatorname="'+res.data[k].creatorName+'" data-createtime="'+res.data[k].createTime+'">'+res.data[k].code+'</option>'
                                }
                                $("#loadPlanId").html(html);
                                form.render('select');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET',false);
                    }
                    var html=getDemandHtml();
                    $("#demandIdTd").html(html);
                    form.render();
                    getDistributionDemandData();




                    form.on('select(type)',function(selectData){
                        if(selectData.value!='selfLoad'){
                            var html=getDemandHtml();
                            $("#demandIdTd").html(html);
                            form.render();
                            getDistributionDemandData();
                            $("#creatorTd").html('');
                            $("#createTimeTd").html('');
                        }else{
                            $("#demandIdTd").html('')
                            form.render();
                            getLoadPlanData();
                        }
                    })

                    form.on('select(demandId)',function(selectData){
                        if(selectData.value!=''){
                            getLoadPlanData(selectData.value);
                            $("#creatorTd").html('');
                            $("#createTimeTd").html('');
                        }
                    })

                    form.on('select(loadPlanId)',function(selectData){
                        var type=$("form select[name='type']").val();
                        if(selectData.value!=''){
                            var creatorName=$("#loadPlanId option:selected").data('creatorname');
                            var createTime=$("#loadPlanId option:selected").select().data('createtime');
                            $("#creatorTd").html(creatorName);
                            $("#createTimeTd").html(createTime);
                            loadPlanData={type:type,loadPlanId:selectData.value}
                            getLoadPlanDetailTable(loadPlanData);
                        }
                    })

                    form.val('orderReceiveForm', data);
                    // 表单提交事件
                    form.on('submit(orderReceiveFormSubmit)', function (d) {
                        if(receiveConfirmLabels.length==0){
                            layer.msg('请先扫描领货的包装条码', {icon: 2});
                            return false;
                        }
                        layer.load(2);
                        d.field.labelList=receiveConfirmLabels;
                        var loadPlanDetailList=[];
                        loadPlanDetails.forEach(item=>{
                            if(item.quantityReceive){
                                loadPlanDetailList.push(item)
                            }
                        })
                        d.field.loadPlanDetailList=loadPlanDetailList;
                        admin.req(data ? 'enterprisecab/order/admin/orderReceive/update' : 'enterprisecab/order/admin/orderReceive/save', JSON.stringify(d.field), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('orderReceiveTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        },'POST',true,'application/json; charset=utf-8');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/order/admin/orderReceive/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#orderReceiveBtnExport').click(function () {
            var checkRows = table.checkStatus('orderReceiveTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>